package com.hamu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hamu.mapper.ArticleMapper;
import com.hamu.mapper.ArticleTagMapper;
import com.hamu.mapper.TagMapper;
import com.hamu.pojo.Article;
import com.hamu.pojo.PageBean;
import com.hamu.service.ArticleService;
import com.hamu.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

//    添加文章
    @Override
    public void add(Article article) {
        //补充属性值
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer)map.get("id");
        article.setCreateUser(userId);
        articleMapper.add(article);
    }
//文章列表
    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state,List<String> tagNames, List<String> tagIds) {
        //创建PageBean对象
        PageBean<Article> pb = new PageBean<>();
        //开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper查询
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer)map.get("id");
        List<Article> as = articleMapper.list(userId,categoryId,state,tagNames,tagIds);
        //Page中提供了方法，可以获取PageHelper分页查询后得到的总记录条数和当前页面数
        Page<Article> p =(Page<Article>) as;
        //把数据填充到PageBean对象
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
    @Override
    public PageBean<Article> listAll(Integer pageNum, Integer pageSize, Integer categoryId, String state,List<String> tagNames, List<String> tagIds){
        //创建PageBean对象
        PageBean<Article> pb = new PageBean<>();
        //开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);
        List<Article> as = articleMapper.listAll(categoryId,state,tagNames,tagIds);
        Page<Article> p =(Page<Article>) as;
        //把数据填充到PageBean对象
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
//搜索
    @Override
    public PageBean<Article> searchList(Integer pageNum, Integer pageSize,  Integer categoryId, String state,List<String> tagNames, List<String> tagIds,String query) {
        PageBean<Article> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleMapper.searchArticles(categoryId,state,tagNames,tagIds,query);
        Page<Article> p = (Page<Article>) articles;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
//id查找
    @Override
    public Article findById(Integer id) {
        return articleMapper.findById(id);
    }
//更新
    @Override
    public void update(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.update(article);
    }
//删除
    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }


}
